

/**
 * Created with IntelliJ IDEA.
 * User: mihir
 * Date: 9/24/13
 * Time: 6:18 PM
 * To change this template use File | Settings | File Templates.
 */
package com.twu.biblioteca;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;

public class Library {

    Member user = new Member();
    ArrayList<Item> bookCollection = new ArrayList<Item>();
    ArrayList<Item> movieCollection = new ArrayList<Item>();
    ArrayList<Member> members = new ArrayList<Member>();

    public  void addItem(){
        Item tempItemObj = new Item();
        tempItemObj.getDetails();
        if(tempItemObj.itemCategory == "Book"){
            bookCollection = updateLibrary(bookCollection, tempItemObj);

        }
        else{
            movieCollection = updateLibrary(movieCollection,tempItemObj);
        }

    }
    public ArrayList updateLibrary(ArrayList<Item> collection, Item tempItemObj){
        if(collection.contains(tempItemObj)){
            int index = collection.indexOf(tempItemObj);
            tempItemObj = collection.get(index);
            tempItemObj.QuantityTotal = tempItemObj.QuantityTotal + 1;
            tempItemObj.QuantityAtLib = tempItemObj.QuantityAtLib + 1;
        }
        else{
            collection.add(tempItemObj);
        }
        return collection;
    }


    public int searchItem(String itemName, String category){
        if(category == "Book"){
            return searchInCollection(bookCollection, itemName);
        }
        else{
            return searchInCollection(movieCollection, itemName);
        }
    }

    private int searchInCollection(ArrayList<Item> collection, String itemName) {
        Item tempItemObj = new Item();
        int length = collection.size();
        for(int i=0;i<length;i++){
            tempItemObj = collection.get(i);
            if(tempItemObj.itemName == itemName && tempItemObj.QuantityAtLib > 1){
                System.out.println("\n" + tempItemObj.itemCategory + " is available");
                tempItemObj.QuantityAtLib = tempItemObj.QuantityAtLib -1;
                return 1;
            }
        }
        return 0;
    }

    public void reserveItem(){
        System.out.println("Enter category Book or Movie - ");
        Scanner user_input = new Scanner( System.in );
        String itemCategory = user_input.next();
        System.out.println("\nEnter Item Name - ");
        String bookName = user_input.next();
        int availability = searchItem(bookName, itemCategory);
        if(availability == 1){

            System.out.println("\nItem has been reserved. Thank You! Enjoy the day.");
        }
        else{
            System.out.println("Sorry we don't have that Item yet.");
        }
    }

    public void listAllItems(String itemCategory){
        if(itemCategory == "Book"){
            listCollection(bookCollection);
        }
        else{
            listCollection(movieCollection);
        }

    }
    public Library(){
        login();
    }
    public void listCollection(ArrayList<Item> collection) {
        int length = collection.size();
        Item tempBookobj = new Item();
        for(int i=0;i<length;i++){
            tempBookobj = collection.get(i);
            System.out.println(tempBookobj.itemName);
        }
    }

    public int login(){
        System.out.println("Enter Username/Library Number - ");
        Scanner loginInput = new Scanner(System.in);
        int userName = loginInput.nextInt();
        Console console =  System.console();   //Can only be run on Java console, won't work for an IDE
        if(console == null){
            System.out.println("Please use a Java console, program won't work in an IDE");
            System.exit(0);
        }
        char passwordArray[] = console.readPassword("Enter password - ");
        if(user.validateUserName(userName) == 1){
            user.userName = userName;
            user.password = passwordArray;

        }

        else{
            generateLoginError();
            return 0;
        }
        if(checkUser(user) == 1){
            return 1;
        }
        else{
            generateLoginError();
            return 0;
        }

    }
    public int checkUser(Member user){
        Member tempObj = new Member();
        int length = members.size();
        for(int i=0;i<length;i++){
            tempObj = members.get(i);
            if(tempObj.userName == user.userName && tempObj.password.equals(user.password)){
                return 1;
            }
        }
        return 0;
    }
    public void generateLoginError(){
        System.out.println("Invalid Username/Password");
    }

    public void userMessage(){
        System.out.println("\nName - " + user.usersname + "\nEmail - " + user.email + "\nPhone Number - " + user.phonenumber);
    }

}
